package fr.cephb.joperon.core.bio;


/** various method for the AcidNucleicUtil */
public class AcidNucleicUtil
	{
	/** Mario Folglio's method for encoding bases with an int*/
	public static int
	encode(int base)
		{
		switch(base)
			{
			case 'A': return  1;
			case 'C': return  2;
			case 'G': return  3;
			case 'T': return  4;
			case 'B': return  5;
			case 'D': return  6;
			case 'H': return  7;
			case 'K': return  8;
			case 'M': return  9;
			case 'R': return 10;
			case 'S': return 11;
			case 'V': return 12;
			case 'W': return 13;
			case 'Y': return 14;
			case '-': return 15;
			case ' ': return 16;
			case 'X': return 17;
			default : return  0;
			}
		}

	/** Mario Folglio's method for decoding bases with an int
	 * @param  base the base
	 * @param rep is it repeat ? yes then lowercase
	 * 
	 * */
	public static char
	decode(int base, boolean rep)
		{
		char c= decode(base);
		return rep?Character.toLowerCase(c):c;
		}
	
	/** Mario Folglio's method for decoding bases with an int
	 * @param  base the base
	 * 
	 * */
	public static char
		decode(int base)
			{
			switch(base)
				{
				case  0: return 'N';
				case  1: return 'A';
				case  2: return 'C';
				case  3: return 'G';
				case  4: return 'T';
				case  5: return 'B';
				case  6: return 'D';
				case  7: return 'H';
				case  8: return 'K';
				case  9: return 'M';
				case 10: return 'R';
				case 11: return 'S';
				case 12: return 'V';
				case 13: return 'W';
				case 14: return 'Y';
				case 17: return 'X';
				case 15: return '-';
				default: return ' ';
				}
			}
	
	/** Mario Folglio's method for decoding the compelemntary from an int
	 * @param  base the base
	 * @param rep is it repeat ? yes then lowercase
	 * */
	public static char
	decode_complementaty(int base, boolean rep)
		{
		switch(base)
			{
			case  0: if (rep) return 'n'; else return 'N';
			case  1: if (rep) return 't'; else return 'T';
			case  2: if (rep) return 'g'; else return 'G';
			case  3: if (rep) return 'c'; else return 'C';
			case  4: if (rep) return 'a'; else return 'A';
			case  5: if (rep) return 'v'; else return 'V';
			case  6: if (rep) return 'h'; else return 'H';
			case  7: if (rep) return 'd'; else return 'D';
			case  8: if (rep) return 'm'; else return 'M';
			case  9: if (rep) return 'k'; else return 'K';
			case 10: if (rep) return 'y'; else return 'Y';
			case 11: if (rep) return 's'; else return 'S';
			case 12: if (rep) return 'b'; else return 'B';
			case 13: if (rep) return 'w'; else return 'W';
			case 14: if (rep) return 'r'; else return 'R';
			case 17: if (rep) return 'x'; else return 'X';
			case 15: return '-';
			default: return ' ';
			}
		}
	
	
	/** Mario Folglio's method for converting an set of alleles to a char encoding
	 * @param alleles the alleles e.g. AG AA AC etc...
	 * */
	static public char 
	dnastr_alleles_to_iupac(String alleles)
		{
		int i=0, n=0;
	
		if (alleles.indexOf('-')!=-1) return 'N';
	
		
		while(i< alleles.length())
			{
			if (alleles.charAt(i) == '/') n = 0;
			else n++;
			if (n > 1) return 'N';
			i++;
			}
	
		     if (strchr(alleles,'A') && strchr(alleles,'C') && strchr(alleles,'G') && strchr(alleles,'T')) return 'N';
		else if (strchr(alleles,'A') && strchr(alleles,'C') && strchr(alleles,'G')) return 'V';
		else if (strchr(alleles,'A') && strchr(alleles,'C') && strchr(alleles,'T')) return 'H';
		else if (strchr(alleles,'A') && strchr(alleles,'G') && strchr(alleles,'T')) return 'D';
		else if (strchr(alleles,'C') && strchr(alleles,'G') && strchr(alleles,'T')) return 'B';
		else if (strchr(alleles,'A') && strchr(alleles,'C')) return 'M';
		else if (strchr(alleles,'A') && strchr(alleles,'G')) return 'R';
		else if (strchr(alleles,'A') && strchr(alleles,'T')) return 'W';
		else if (strchr(alleles,'C') && strchr(alleles,'G')) return 'S';
		else if (strchr(alleles,'C') && strchr(alleles,'T')) return 'Y';
		else if (strchr(alleles,'G') && strchr(alleles,'T')) return 'K';
		else return 'N';
		}
	
	private static boolean strchr(String alleles,char c)
		{
		return alleles.indexOf(c)!=-1;
		}
	
	//was 
	/** Mario Folglio's method for encoding an allele to a code was in common_gtypes_decode
	 * @param code the encoding
	 * @return  the alleles e.g. AG AA AC etc...
	 * */
	public static String decode_genotype(int code)
	{
	switch(code)
		{
		case   1: return ("AA");
		case   2: return ("CC");
		case   3: return ("GG");
		case   4: return ("TT");
		case   5: return ("AC");
		case   6: return ("AG");
		case   7: return ("AT");
		case   8: return ("CG");
		case   9: return ("CT");
		case  10: return ("GT");
		case  15: return ("CA");
		case  16: return ("GA");
		case  17: return ("TA");
		case  18: return ("GC");
		case  19: return ("TC");
		case  20: return ("TG");
		case  21: return ("00");
		case  22: return ("AN");
		case  23: return ("CN");
		case  24: return ("GN");
		case  25: return ("TN");
		
		
		case 26: return "N";
		case 27: return "A";
		case 28: return "C";
		case 29: return "G";
		case 30: return "T";
		case 31: return "0";
		case 32: return "A-";
		case 33: return "C-";
		case 34: return "G-";
		case 35: return "T-";
		case 36: return "-A";
		case 37: return "-C";
		case 38: return "-G";
		case 39: return "-T";
		case 40: return "--";
		
		
		default : return ("NN");
		}
	}
	
	
	}
